<div class="main layui-clear">
    <div class="wrap">
        <div class="content detail">
            <h1>
                {$q_data['title']}
            </h1>
            <div data-id="4281" class="fly-tip fly-detail-hint">
            <?php if($q_data['qext']['top'] != '0') {?>
                <span class="fly-tip-stick">
                    置顶帖
                </span>
            <?php } elseif($q_data['qext']['excellent'] != '0') {?>
                <span class="fly-tip-jing">
                    精帖
                </span>
            <?php } elseif($q_data['qext']['get_great_ans'] != '0') {?>
                <span class="fly-tip-jie">
                    已采纳
                </span>
            <?php } elseif($q_data['qext']['close'] == '0') {?>
                <span>
                    未结贴
                </span>
            <?php } ?>
                <div class="fly-list-hint">
                    <i title="回答" class="iconfont">
                        
                    </i>
                    {$q_data['qext']['num_of_ans']}
                    <i title="人气" class="iconfont">
                        
                    </i>
                    {$q_data['qext']['views']}
                </div>
            </div>
            <div class="detail-about">
                <a href="{:U('home/ucenter/index',array('uid'=>$u_data['uid']))}" class="jie-user">
                    <img alt="{$u_data['uname']}" src="__ROOT__/{$u_data['uext']['avatar']}">
                    <cite>
                        {$u_data['uname']}
                        <em>
                            发布于<?php echo timeFormat($q_data['qtime']);?>
                        </em>
                    </cite>
                </a>
                <div data-id="4281" id="LAY_jieAdmin" class="detail-hits">
                    <span style="color:#FF7200">
                        悬赏：5飞吻
                    </span>
                    <span data-type="add" type="collect" class="layui-btn layui-btn-mini jie-admin">
                        收藏
                    </span>
                </div>
            </div>
            <div style="margin-bottom: 20px;" class="detail-body photos">
                <?php echo htmlspecialchars_decode($q_data['content']);?>
            </div>
            <a name="comment">
            </a>
            <h2 class="page-title">
                热忱回答
                <span>
                    （
                    <em id="jiedaCount">
                        {$q_data['qext']['num_of_ans']}
                    </em>
                    ）
                </span>
            </h2>
            <ul id="jieda" class="jieda photos">
                <?php if(!empty($a_data)) {
                    foreach ($a_data as $value) { ?>
                        <li class="jieda-daan" data-id="{$value['aid']}">
                            <a name="item-1476470300661">
                            </a>
                            <div class="detail-about detail-about-reply">
                                <a href="{:U('home/ucenter/index',array('uid'=>$value['uid']))}" class="jie-user">
                                    <img alt="{$value['user']['uname']}" src="__ROOT__/{$value['uext']['avatar']}" layer-index="1">
                                    <cite>
                                        <i>
                                            {$value['user']['uname']}
                                        </i>
                                    </cite>
                                </a>
                                <div class="detail-hits">
                                    <span>
                                        {:timeFormat($value['atime'])}
                                    </span>
                                </div>
                                <?php if ($q_data['qext']['get_great_ans'] == $value['aid']) { ?>
                                    <i title="最佳答案" class="iconfont icon-caina"></i>
                                <?php } ?>
                            </div>
                            <div class="detail-body jieda-body">
                                {:htmlspecialchars_decode($value['content'])}
                            </div>
                            <div class="jieda-reply">
                                <span type="zan" class="jieda-zan zaned">
                                    <i class="iconfont icon-zan"></i>
                                    <em>{$value['aext']['praise']}</em>
                                </span>
                                <span type="reply">
                                    <i class="iconfont icon-svgmoban53"></i>
                                    回复
                                </span>
                                <?php if ($value['uid'] != $_SESSION['uid'] && $_SESSION['uid'] == $q_data['uid'] && $q_data['close'] == 0) { ?>
                                    <div class="jieda-admin">
                                        <span class="jieda-accept" type="accept">采纳</span>
                                    </div>
                                <?php } ?>
                            </div>
                        </li>
                <?php }
                } else { ?>
                <li class="fly-none">
                    没有任何回答
                </li>
                <?php } ?>
            </ul>
            <div id="laypage-main"></div>
            <div class="layui-form layui-form-pane">
                <form method="post" action="">
                    <div class="layui-form-item layui-form-text">
                        <div class="layui-input-block">
                            <textarea style="height: 150px;" class="layui-textarea fly-editor" placeholder="我要回答"
                            lay-verify="content" required="" name="content" id="L_content"></textarea>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <input type="hidden" value="4281" name="jid">
                        <button lay-submit="" lay-filter="asubmit" class="layui-btn">
                            提交回答
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <div class="edge">
        <include file="Common:postranking" />
    </div>
</div>
<script>
// 收藏
$('.jie-admin').click(function(){
    $(this).toggleClass('layui-btn-danger');
    if ($(this).hasClass('layui-btn-danger')) {
        $(this).text('取消收藏');
        $.ajax({
            url: '{:U("home/ucenter/collection")}',
            type: 'POST',
            dataType: 'json',
            data: {do: 'addCollection',qid: '{$q_data["qid"]}'},
        })
        .done(function(response) {
            if (response) {
                console.log(response);
            }
        })
        .fail(function() {
            console.log("error");
        });
    } else {
        $(this).text('收藏');
        $.ajax({
            url: '{:U("home/ucenter/collection")}',
            type: 'POST',
            dataType: 'json',
            data: {do: 'delCollection',qid: '{$q_data["qid"]}'},
        })
        .done(function(response) {
            if (response) {
                console.log(response);
            }
        })
        .fail(function() {
            console.log("error");
        });
    }
});
// 点赞
$('body').on('click','.jieda-zan',function(){
    var aid = $(this).parent().parent().attr('data-id');
    if ($(this).hasClass('zaned')) {
        var zans = parseInt($(this).children('em').text())+1;
        $(this).children('em').text(zans);
        $.ajax({
            url: '{:U("home/answer/praise")}',
            type: 'POST',
            dataType: 'json',
            data: {do: 'addPraise',praise: zans,aid: aid},
        })
        .done(function(response) {
            if (response) {
                console.log(response);
            }
        })
        .fail(function(response) {
            console.log(response);
        });
    } else {
        var zans = parseInt($(this).children('em').text())-1;
        $(this).children('em').text(zans);
        $.ajax({
            url: '{:U("home/answer/praise")}',
            type: 'POST',
            dataType: 'json',
            data: {do: 'delPraise',praise: zans,aid: aid},
        })
        .done(function(response) {
            if (response) {
                console.log(response);
            }
        })
        .fail(function(response) {
            console.log(response);
        });
    }
    $(this).toggleClass('zaned');
});
// 采纳最佳答案
$('.jieda-accept').click(function () {
    var aid = $(this).parent().parent().parent().attr('data-id');
    var qid = {$q_data['qid']};
    
    $.ajax({
        url: '{:U("home/answer/greatAns")}',
        type: 'POST',
        dataType: 'json',
        data: {aid: aid,qid: qid},
    })
    .done(function(response) {
        if (response == 1) {
            layer.msg('操作成功', {icon: 6}, function () {
                location.href = "{:U('home/question/qpage',array('qid'=>$q_data['qid']))}";
            });
        };
    })
    .fail(function() {
        console.log("error");
    });
    
});

layui.use(['form', 'layedit', 'laypage'],function() {
    var form = layui.form(),
    layer = layui.layer,
    layedit = layui.layedit,
    laypage = layui.laypage;
    var actname = '{$actname}';

    layedit.set({
        uploadImage: {
            url: "{:U('home/answer/aimgupload')}" //接口url
            ,
            type: 'post' //默认post
        }
    });

    //创建一个编辑器
    editIndex = layedit.build('L_content', {
        tool: ['face', 'image', 'link', 'code', 'yulan']
    });

    // 回复
    $('span[type="reply"]').click(function(){
        var userlink = $(this).parent().siblings('.detail-about-reply').children('.jie-user').attr('href');
        var content = '@<a href="'+userlink+'">'+$(this).parent().siblings('.detail-about-reply').children('.jie-user').children('cite').children('i').text()+'</a>&nbsp;';
        content += layedit.getText(editIndex);
        $('#L_content').val(content);
        // 刷新编辑器
        editIndex = layedit.build('L_content', {
            tool: ['face', 'image', 'link', 'code', 'yulan']
        });
    });

    //自定义验证规则
    form.verify({
        content: function(obj) {
            content = layedit.getContent(editIndex);
            if (content.length < 1) {
                return "内容不能为空";
            } else {
                layedit.sync(editIndex);
            }
        }
    });

    //监听答题提交
    form.on('submit(asubmit)',function(data) {
        $('#L_content').val(layedit.getContent(editIndex));
        data.field.qid = {$q_data['qid']};
        var anplus = parseInt($('#jiedaCount').text())+1;
        data.field.num_of_ans = anplus;
        // 刷新编辑器
        editIndex = layedit.build('L_content', {
            tool: ['face', 'image', 'link', 'code', 'yulan']
        });
        $.ajax({
            url: "{:U('home/answer/apost')}",
            type: 'POST',
            dataType: 'json',
            data: data.field,
        }).done(function(response) {
            if (response.error) {
                if (!response.skip) {
                    layer.msg(response.error, {
                        icon: 5
                    });
                } else {
                    layer.msg(response.error, {
                        icon: 5
                    },function(){
                        location.href = "{:U('home/login/index')}";
                    });
                }
            } else if (response.info) {
                layer.msg(response.info, {
                    icon: 6
                });
                var str = '<li class="jieda-daan" data-id="'+response.aid+'"><a name="item-1483513264795"></a><div class="detail-about detail-about-reply"><a class="jie-user"href="{:U("home/ucenter/index", array("uid"=>$_SESSION["uid"]))}"><img src="__ROOT__/{$_SESSION["avatar"]}"alt="{$_SESSION["nickname"]}"layer-index="0"><cite><i>{$_SESSION["nickname"]}</i></cite></a><div class="detail-hits"><span>'+response.atime+'</span></div></div><div class="detail-body jieda-body">'+data.field.content+'</div><div class="jieda-reply"><span class="jieda-zan zaned"type="zan"><i class="iconfont icon-zan"></i><em>0</em></span><span type="reply"><i class="iconfont icon-svgmoban53"></i>回复</span><div class="jieda-admin"></div></div></li>';

                if ($('.fly-none')) {
                    $('.fly-none').remove();
                    $('#jieda').append(str);
                    $('#jiedaCount').text(anplus);
                } else {
                    $('#jieda').append(str);
                    $('#jiedaCount').text(anplus);
                }
            }
        }).fail(function() {
            layer.msg("数据传输失败", {
                icon: 5
            });
        });
        return false;
    });
    // 分页
    laypage({
        cont: $('#laypage-main'),
        pages: "{$allpages}",
        curr: function() {
            var page = "{$nowpage}"; // 当前页(后台获取到的)
            return page ? page: 1; // 返回当前页码值
        } (),
        jump: function(e, first) { //触发分页后的回调(单击页码后)
            if (!first) { //一定要加此判断，否则初始时会无限刷新
                $.ajax({
                    url: "{:U('home/question/ansPage')}",
                    type: 'POST',
                    dataType: 'json',
                    data: {apage: e.curr, qid: {$q_data['qid']}},
                })
                .done(function(response) {
                    var list = response.page_list;
                    var list = list.replace(/ROOT/g,'__ROOT__');
                    $('#jieda').empty();
                    $('#jieda').append(list);
                })
                .fail(function() {
                    console.log("error");
                });
            }
        }
    });
});
</script>